package com.thinkgem.jeesite.cn.yy.action;

import com.thinkgem.jeesite.cn.yy.entity.ColAction;
import com.thinkgem.jeesite.cn.yy.service.ColRecordService;
import com.thinkgem.jeesite.cn.yy.utils.Paging;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 数据表字段操作信息记录Action
 * Created by Administrator on 2017/11/27 0027.
 * yzw
 */
@Controller
@RequestMapping("ColRecord")
public class ColRecordAction {

    @Autowired
    private ColRecordService colRecordService;

    /**
     * 获取数据字段操作信息所有记录
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("getColRecords")
    public void getColRecords(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int count = colRecordService.getCount();

        if(count == 0){
            response.getWriter().println("{\"message\":\"没有数据表信息(没有权限)!\"}");
            return;
        }
        int first=Integer.parseInt(request.getSession().getServletContext().getAttribute("pageSize").toString());
        Paging paging = new Paging(0,first,count);
        List<ColAction> colList = colRecordService.getColRecords(paging);
        response.getWriter().println(JSONArray.fromObject(paging).toString());
        response.getWriter().println(JSONArray.fromObject(colList).toString());
        response.getWriter().println("{\"message\":\"ok\"}");
    }

    /**
     * 获取数据字段操作信息所有记录
     * 上一页 下一页
     * @param currentPage
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("getColRecordsPage")
    public void getColRecordsPage(int currentPage,HttpServletRequest request, HttpServletResponse response) throws IOException {
        Paging paging = (Paging) request.getSession().getAttribute("paging");
        paging.setFirstPage((currentPage-1)*paging.getLastPage());
        paging.setCountPage(currentPage);

        List<ColAction> colList = colRecordService.getColRecords(paging);

        response.getWriter().println(JSONArray.fromObject(colList).toString());
        response.getWriter().println(JSONArray.fromObject(paging).toString());
        response.getWriter().print("{\"message\":\"ok\"}");
    }

    /**
     * 通过Tid获取某一表的所有操作记录
     * @param tab_id
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("getColRecordByCid")
    public void getColRecordByCid(int tab_id,HttpServletRequest request, HttpServletResponse response) throws IOException {
        if(tab_id != 0){
            List<ColAction> colList = colRecordService.getColRecordByCid(tab_id);
            response.getWriter().println(JSONArray.fromObject(colList).toString());
            response.getWriter().print("{\"message\":\"ok\"}");
        }
    }

    /**
     * 通过表名查询所有表字段的操作信息
     * @param tab_name
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("getColRecordByTname")
    public void getColRecordByTname(String tab_name,HttpServletRequest request,HttpServletResponse response) throws IOException {
        if(!"".equals(tab_name.trim()) && tab_name != null){
            List<ColAction> colList = colRecordService.getColRecordByTname(tab_name);
            response.getWriter().println(JSONArray.fromObject(colList).toString());
            response.getWriter().print("{\"message\":\"ok\"}");
        }
    }


}
